
'''作业1'''
import requests

url_api = 'https://api.douban.com/v2/movie/1292052?apikey=0df993c66c0c636e29ecbb5344252a4a'

req = requests.get(url_api, headers={'user-agent':'chrome'})
data = req.json()
req_image_url = data['image']
req_image = requests.get(req_image_url).content

with open('image.jpg','wb') as f:
    f.write(req_image)



'''作业2'''
import requests,csv
with open('film.csv','a')as f:
    url_api =[ ('https://api.douban.com/v2/movie/top250?start=%d&apikey=0df993c66c0c636e29ecbb5344252a4a'%i)for i in range(0,250,20)]
    for c in range(0,len(url_api)):
        req = requests.get(url_api[c], headers={'user-agent':'chrome'})
        data = req.json()['subjects']
        for j in range(0,len(data)):
            average = data[j]["rating"]["average"]
            title = data[j]["title"]
            casts = []
            for k in range(0,len(data[j]["casts"])):
                casts.append(data[j]["casts"][k]["name"])
            casts = ','.join(casts)
            image = data[j]["images"]['small']
            req_image = requests.get(image).content
            with open('%s.jpg'%title,'wb') as f1:
                f1.write(req_image)
            id = data[j]["id"]
            row = [id,title,average,casts,image]
            f_csv = csv.writer(f)
            f_csv.writerow(row)


